package com.zhuge_new.Activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jungly.gridpasswordview.GridPasswordView;
import com.jungly.gridpasswordview.PasswordType;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.Dialog.EditDialog;
import com.zhuge_new.Dialog.PassWor_Dialog;
import com.zhuge_new.Dialog.PatmentDialog;
import com.zhuge_new.Dialog.WheelViewDialog;
import com.zhuge_new.R;
import com.zhuge_new.bean.CardFlBean;
import com.zhuge_new.bean.CardListBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.SoftKeyboardUtil;
import com.zhuge_new.untils.Tailuntils;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by lenovo on 2018-09-05.
 */

public class GatherActivity extends BaseActivity implements View.OnClickListener {

    private ArrayList<String> list_bank = new ArrayList<String>();
    private ArrayList<String> list_term = new ArrayList<String>();
    private ArrayList ratelist = new ArrayList();
    private TextView gather_bank;
    private TextView gather_money;
    private TextView gather_show;
    private CardListBean cardListBean;
    private String rate;
    private String logourl;
    private ImageView gather_logo;
    private String creditcard;
    private String creditcardId;
    private String soure;
    private EditText gather_et;
    private CardFlBean cardFlBean;
    private String channel;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gather);
        Intent intent = getIntent();
        cardListBean = (CardListBean) intent.getSerializableExtra("cardListBean");
        list_bank = intent.getStringArrayListExtra("list_bank");
        init_title();
        getCard();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText(R.string.min_collection);
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        LinearLayout select_bank = findViewById(R.id.select_bank);
        Button confirm = findViewById(R.id.confirm);
        gather_logo = findViewById(R.id.gather_logo);
        gather_bank = findViewById(R.id.gather_bank);
        gather_money = findViewById(R.id.gather_money);
        RelativeLayout gather_mode = findViewById(R.id.gather_mode);
        gather_et = findViewById(R.id.gather_et);
        gather_show = findViewById(R.id.gather_show);
        gather_bank.setText(list_bank.get(0));
        for (int i = 0; i < cardListBean.getCreditcards().size(); i++) {
            String credit = cardListBean.getCreditcards().get(i).getCreditcard();
            String bank = gather_bank.getText().toString();
            if (credit.substring(credit.length() - 4, credit.length()).equals(bank.substring(bank.length() - 4, bank.length()))) {
                logourl = cardListBean.getCreditcards().get(i).getLogoUrl();
            }
        }
        Glide.with(baseContext).load(BaseHttp.baseImg1 + logourl).into(gather_logo);
        iv_back.setOnClickListener(this);
        select_bank.setOnClickListener(this);
        gather_money.setOnClickListener(this);
        gather_mode.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_bank:
                bankDialog();
                break;
            case R.id.gather_money:
                importDiaolog();
                break;
            case R.id.gather_mode:
                payment_term();
                break;
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(GatherActivity.class);
                finish();
                break;
            case R.id.confirm:
                if (Integer.parseInt(gather_money.getText().toString()) < 1) {
                    MToast.makeTextShort(this, "收款金额最低150.00元").show();
                } else if (gather_show.getText().toString().equals("")) {
                    MToast.makeTextShort(this, "请选择收款方式").show();
                } else {
                    submit();
                }
                break;
        }

    }

    //收款方式判断
    private void payment_term() {
        list_term.clear();
        list_term.add("快速收款");
        for (int i = 0; i < cardListBean.getCreditcards().size(); i++) {
            String credit = cardListBean.getCreditcards().get(i).getCreditcard();
            String bank = gather_bank.getText().toString();
            if (credit.substring(credit.length() - 4, credit.length()).equals(bank.substring(bank.length() - 4, bank.length()))) {
                if (cardListBean.getCreditcards().get(i).getChannelType().equals("GAO")) {

                }else {
                    list_term.add("在线收款");
                }
            }
        }
        PatmentDialog patmentDialog = new PatmentDialog(GatherActivity.this, list_term);
        patmentDialog.setTitle("请选择收款方式");
        patmentDialog.setOnSelectedListener(new PatmentDialog.OnSelectedListener() {

            @Override
            public void getData(String data) {
                gather_show.setText(data);
            }
        });
        patmentDialog.show();

    }

    //写入收款金额对话框
    private void importDiaolog() {
        final EditDialog editDialog = new EditDialog(GatherActivity.this);
        editDialog.setTitle("请输入收款金额");
        editDialog.setYesOnclickListener("确定", new EditDialog.onYesOnclickListener() {
            @Override
            public void onYesClick(String phone) {
                if (TextUtils.isEmpty(phone)) {
                    MToast.makeTextShort(GatherActivity.this, "请输入收款金额").show();
                } else {
                    gather_money.setText(phone);
                    SoftKeyboardUtil.hideSoftKeyboard(GatherActivity.this);
                    editDialog.dismiss();
                }
            }
        });
        editDialog.setNoOnclickListener("取消", new EditDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                editDialog.dismiss();
            }
        });
        editDialog.show();

    }

    //请求信用卡列表接口
    public void getCard() {
        OkGo.<String>post(BaseHttp.creditcard_datas)
                .headers("token", PreferencesUtils.getString(this, "token"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        cardListBean = (CardListBean) new DataParse().getBean(response.body(), CardListBean.class, 0);
                    }
                });
    }

    //收款卡片对话框
    private void bankDialog() {
        WheelViewDialog wheelViewDialog = new WheelViewDialog(GatherActivity.this, list_bank);
        wheelViewDialog.setOnSelectedListener(new WheelViewDialog.OnSelectedListener() {
            @Override
            public void getData(String data) {
                gather_bank.setText(data);
                for (int i = 0; i < cardListBean.getCreditcards().size(); i++) {
                    String name = cardListBean.getCreditcards().get(i).getBank();
                    if (name.equals(data.substring(0, 4))) {
                        logourl = cardListBean.getCreditcards().get(i).getLogoUrl();
                    }
                }
                Glide.with(baseContext).load(BaseHttp.baseImg1 + logourl).into(gather_logo);
            }
        });
        wheelViewDialog.show();
    }

    //获取费率接口
    private void submit() {
        for (int i = 0; i < cardListBean.getCreditcards().size(); i++) {
            String credit = cardListBean.getCreditcards().get(i).getCreditcard();
            String bank = gather_bank.getText().toString();
            if (credit.substring(credit.length() - 4, credit.length()).equals(bank.substring(bank.length() - 4, bank.length()))) {
                creditcard = cardListBean.getCreditcards().get(i).getCreditcard();
                creditcardId = cardListBean.getCreditcards().get(i).getCreditcardId();
            }
        }
        OkGo.<String>post(BaseHttp.repaymentPlanRate)
                .tag(this)
                .headers("token", PreferencesUtils.getString(this, "token"))
                .params("creditcard", creditcard)
                .params("creditcardId", creditcardId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        cardFlBean = (CardFlBean) new DataParse().getBean(response.body(), CardFlBean.class, 0);
                        if (cardFlBean.getData().size() == 1) {
                            rate = cardFlBean.getData().get(0).getRate();
                        } else {
                            data_dialog(cardFlBean);
                        }
                    }
                });
    }

    //选择费率对话框
    private void data_dialog(CardFlBean cardFlBean) {
        ratelist.clear();
        for (int i = 0; i < cardFlBean.getData().size(); i++) {
            ratelist.add(cardFlBean.getData().get(i).getRate());
        }
        PatmentDialog patmentDialog = new PatmentDialog(GatherActivity.this, (ArrayList<String>) ratelist);
        patmentDialog.setTitle("请选择费率");
        patmentDialog.setOnSelectedListener(new PatmentDialog.OnSelectedListener() {
            @Override
            public void getData(String data) {
                rate = data;
                getbtnCard();
            }
        });
        patmentDialog.show();
    }

    //提交数据
    public void getbtnCard() {
        if (rate != null) {

            for (int i = 0; i < cardFlBean.getData().size(); i++) {
                if (rate.equals(cardFlBean.getData().get(i).getRate())) {
                    channel = cardFlBean.getData().get(i).getChannelCode();
                    type = cardFlBean.getData().get(i).getType();
                }
            }
            if (gather_show.getText().toString().equals("快速收款")) {
                soure = "K";
            } else {
                soure = "B";
            }
            PassWor_Dialog dialog=new PassWor_Dialog(baseContext) {

                private ImageView withdraw_close;
                private GridPasswordView gpv_pwd;

                @Override
                public View onCreateView() {
                    View view=View.inflate(baseContext,R.layout.dialog_member,null);
                    gpv_pwd = view.findViewById(R.id.gridview);
                    TextView withdraw_hint=view.findViewById(R.id.withdraw_hint);
                    withdraw_close = view.findViewById(R.id.withdraw_close);
                    withdraw_hint.setTextSize(22f);
                    gpv_pwd.setPasswordType(PasswordType.NUMBER);
                    return view;
                }

                @Override
                public void setUiBeforShow() {
                    withdraw_close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                    gpv_pwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
                        @Override
                        public void onTextChanged(String psw) {
                        }

                        @Override
                        public void onInputFinish(String psw) {
                            OkGo.<String>post(BaseHttp.paypawd_check)
                                    .tag(GatherActivity.this)
                                    .headers("token",PreferencesUtils.getString(baseContext,"token"))
                                    .params("paypawd",psw)
                                    .execute(new StringDialogCallback(baseContext) {
                                        @Override
                                        public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                                            if (msgCode.equals("100")){
                                                getend();
                                                dismiss();
                                            }
                                        }
                                    });
                        }
                    });
                }
            };
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    SoftKeyboardUtil.showSoftInput(GatherActivity.this);
                }
            });
            dialog.show();
        }
    }

    private void getend(){
        OkGo.<String>post(BaseHttp.appYee_repayment)
                .tag(this)
                .headers("token", PreferencesUtils.getString(GatherActivity.this, "token"))
                .params("carno", creditcard)
                .params("source", soure)
                .params("amount", gather_money.getText().toString())
                .params("remark", gather_et.getText().toString())
                .params("channel", channel)
                .params("plat", type)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        if (msgCode.equals("100")){
                            MToast.makeTextShort(baseContext,msg).show();
                            ActivityStack.getScreenManager().popOneActivity(GatherActivity.class);
                        }else {
                            MToast.makeTextShort(baseContext,msg).show();
                        }
                    }
                });
    }
}
